package com.flyee.controller.tools.cartoon.control
{
	import com.flyee.controller.tools.cartoon.AppearanceCartoon;
	import com.flyee.controller.tools.cartoon.model.CreateAppearanceModel;
	import com.flyee.events.AppearanceEvent;
	
	import flash.utils.Dictionary;
	
	public class AppearanceMananger
	{
		private static var instance:AppearanceMananger = null;
		
		//private var infoManagerArr:AppearanceInfoManager = new AppearanceInfoManager();
		private var loaderManager:AppearanceLoadManager = new AppearanceLoadManager();
		
		private var changeAppearanceDic:Dictionary = new Dictionary();
		
		//存放已经组装好的图版
		private var createdAppearanceDic:Dictionary = new Dictionary();
		
		//等待创建图版得对象数组
		private var waitCreateList:Array = new Array();
		
		private var isOpen:Boolean = true;
		public function AppearanceMananger()
		{
			if (instance)
			{
				throw new Error("singleton error!!!");
			}
			instance = this;
		}
		public static function getInstance():AppearanceMananger
		{

			if (instance == null)
			{
				instance = new AppearanceMananger();
			}
			return instance;
		}
		/**
		 * 
		 * @param id   人物ID
		 * @return     1个包含该人物所有装备的2维图片数组 —— array[type][dir]
		 * 
		 */		
		public function getAllAppearance(id:String):Array
		{
			return this.createdAppearanceDic[id]
		}
		public function setAllAppearance(id:String,tagArray:Array):void
		{
			this.createdAppearanceDic[id] = tagArray;
		}
		/**
		 * 
		 * @param tag   
		 * @param t    装备配置文件
		 * 
		 */		
		public function regist(tag:AppearanceCartoon,t:XML = null):void
		{
			var temp:CreateAppearanceModel = new CreateAppearanceModel();
			temp.appearanceCartoon = tag;
			temp.queipXML = t;
			this.waitCreateList.push(temp);
			create();
		}
		public function create():void
		{
			if(this.isOpen)
			{
				if(this.waitCreateList.length > 0)
				{
					this.isOpen = false;
					var temp:CreateAppearanceModel = this.waitCreateList.shift();
					temp.appearanceCartoon.addEventListener(AppearanceEvent.ALL_COMPLETE_APPEARANCE,this.createCompleteHandle);
					this.setInfo(temp);
				}
			}
		}
		private function setInfo(model:CreateAppearanceModel):void
		{
			//
			if(model == null)
			{
				this.isOpen = true;
				return
			}
			var tag:AppearanceCartoon = model.appearanceCartoon;
			//广播消息 说明系统开始初始化换装系统
			tag.getListenner().dispatchEvent(new AppearanceEvent(AppearanceEvent.INIT_APPEARANCE,tag.getID()))
			
			var tempID:String = tag.getID()
			var infoManager:AppearanceInfoManager = new AppearanceInfoManager();
			model.infoManager = infoManager;
			//存储该装备初始化数据；
			changeAppearanceDic[tempID] = model;
			infoManager.setInfo(model);
		}
		public function createCompleteHandle(e:AppearanceEvent):void
		{
			AppearanceLoadManager.getInstance().clear();
			var temp:AppearanceCartoon = e.currentTarget as AppearanceCartoon;
			temp.removeEventListener(AppearanceEvent.ALL_COMPLETE_APPEARANCE,this.createCompleteHandle);
			this.isOpen = true;
			create()
		}
		public function getAppCartoon(id:String):CreateAppearanceModel
		{
			return changeAppearanceDic[id];
		}
		/**
		 *删除指定的换装数据 
		 * @param t
		 * 
		 */		
		public function removeTag(t:AppearanceCartoon):void
		{
			if(changeAppearanceDic[t.getID()] != undefined)
			{
				var temp:CreateAppearanceModel = changeAppearanceDic[t.getID()];
				if(temp)
				{
					temp.clear();
				}
				delete changeAppearanceDic[t.getID()];
			}
		}
		public function clear():void
		{
			for(var i:* in changeAppearanceDic)
			{
				var temp:CreateAppearanceModel = changeAppearanceDic[i];
				temp.clear();
				delete changeAppearanceDic[i];
			}
			changeAppearanceDic = null;
			
			for(var j:* in createdAppearanceDic)
			{
				var temp2:Array = createdAppearanceDic[j];
				temp2.splice(0);
				delete createdAppearanceDic[j];
			}
			createdAppearanceDic = null;
			
			instance = null;
			
			loaderManager.clear();
			loaderManager = null;
			
		}
	}
}